# -*- Mode: Python -*- vi:si:et:sw=4:sts=4:ts=4:syntax=python

from cerbero.utils import shell
from cerbero.utils import needs_xcode8_sdk_workaround

class Recipe(recipe.Recipe):
    name = 'gnutls'
    version = '3.5.18'
    url = 'https://www.gnupg.org/ftp/gcrypt/%(name)s/v%(maj_ver)s/%(name)s-%(version)s.tar.xz'
    tarball_checksum = 'ae2248d9e78747cf9c469dde81ff8f90b56838b707a0637f3f7d4eee90e80234'
    stype = SourceType.TARBALL
    # main library is LGPLv2+ and binaries is GPLv3+ and defined below
    licenses = [{License.LGPLv2Plus: None, License.Misc: ['LICENSE']}]
    configure_options = "--enable-local-libopts --disable-guile --disable-openssl-compatibility \
        --without-p11-kit --enable-static --enable-zlib --enable-shared \
        --disable-doc --disable-tests --with-included-unistring"
    deps = ['zlib', 'nettle', 'libtasn1']
    platform_deps = {
        Platform.WINDOWS: ['proxy-libintl'],
        Platform.ANDROID: ['proxy-libintl'],
        Platform.IOS: ['proxy-libintl'],
        Platform.DARWIN: ['proxy-libintl']
    }
    patches = [name + "/0003-Disable-ncrypt-support.patch",
               name + "/0001-Undefine-__USE_MINGW_ANSI_STDIO-as-otherwise-stdio.h.patch",
               name + "/0001-pgusage-remove-system-call.patch",
               name + "/0001-asm-rename-some-assembly-functions-to-not-conflict-w.patch",
               name + "/0001-Make-headers-MSVC-compatible-when-built-with-GCC.patch",]
    autoreconf = True

    files_libs = ['libgnutls', 'libgnutlsxx']
    files_bins = ['gnutls-cli', 'gnutls-serv', 'gnutls-cli-debug']
    tarball_checksum = 'ae2248d9e78747cf9c469dde81ff8f90b56838b707a0637f3f7d4eee90e80234'
    licenses_bins = [License.GPLv3Plus]
    files_devel = ['lib/pkgconfig/gnutls.pc', 'include/gnutls']
    files_lang = ['gnutls']

    def prepare(self):
        if needs_xcode8_sdk_workaround(self.config):
            self.set_env('ac_cv_func_clock_gettime', 'no')
            self.set_env('ac_cv_func_clock_settime', 'no')
        if self.config.target_platform == Platform.WINDOWS:
            self.configure_options += ' --enable-threads=win32'
            self.can_use_configure_cache = False
        if self.config.target_platform == Platform.DARWIN:
            self.configure_options += ' --disable-hardware-acceleration'
        if self.config.target_platform == Platform.ANDROID:
            self.configure_options += ' --disable-cxx'
            self.files_libs.remove('libgnutlsxx')
            # Fix build with NDK 16
            self.set_env('ac_cv_header_stdatomic_h', 'no')
            v = DistroVersion.get_android_api_version(self.config.target_distro_version)
            if self.config.target_arch in [Architecture.ARM, Architecture.ARMv7, Architecture.X86] and v < 21:
                # FIXME: HACK to make projects compile with NDK 16
                # we fail to compile with -D_FILE_OFFSET_BITS=64
                # because we don't use clang and we use a platform < 21 (Lollipop)
                # See $NDK_ROOT/sysroot/usr/include/sys/mman.h as one example
                self.set_env('ac_cv_func_mmap', 'no')
        if self.config.target_platform == Platform.IOS:
            if self.config.target_arch == Architecture.ARM64:
                self.configure_options += ' --disable-hardware-acceleration'
            self.configure_options += ' --disable-cxx'
            self.files_libs.remove('libgnutlsxx')

    async def configure(self):
        if needs_xcode8_sdk_workaround(self.config):
            # There is no env var that can be set to make the AC_COMPILE_IFELSE
            # macro that checks this evaluate to 'no', so we change the symbol.
            # Edit configure.ac and not configure because we autoreconf later.
            shell.replace(os.path.join(self.build_dir, 'configure.ac'),
                          {'getentropy': 'getentropy_DISABLED_NOT_FOR_MACOS10_12'})
        if self.config.target_platform == Platform.ANDROID:
            # On Android, SIZE_MAX is define under limits.h
            shell.replace(os.path.join(self.build_dir, 'gl', 'read-file.c'),
                {'#include <stdint.h>':
                 '#include <limits.h>'})
        # Call configure from the base class
        await super(recipe.Recipe, self).configure()
